#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "OLED.h"
#include "Serial.h"
#include "Key.h"
#include "Servo.h"
#include "HX711.h"

uint8_t KeyNum;			//定义用于接收键码的变量
uint8_t my_switch = 48;
int16_t Angle = 0;
uint16_t send_time = 0;
uint16_t Lost_weight = 0;

void my_servo(void)
{
		if(my_switch == 0)
		{
		    Angle = 0;
		}
		if(my_switch == 1)
		{
				Angle = 180;

		}
    if(my_switch == 2)
		{
				KeyNum = Key_GetNum();			//获取按键键码
				if (KeyNum == 1)				//按键1按下
				{
					Angle += 30;				//角度变量自增30
					if (Angle > 180)			//角度变量超过180后
					{
						Angle = 180;				//角度变量归零
					}
				}
				if (KeyNum == 2)				//按键1按下
				{
					Angle -= 30;				
					if (Angle < 10)			
					{
						Angle = 0;			
					}
				}
		}
		Servo_SetAngle(Angle);			//设置舵机的角度为角度变量
		OLED_ShowNum(2,8,Angle,3);

}
int main(void)
{
	/*模块初始化*/
	OLED_Init();				//OLED初始化
	Servo_Init();       //舵机初始化
	Key_Init();			//按键初始化
	USART3_init(115200);		//串口初始化
	WiFi_Iot_touchuan();    //连接服务器
	HX711_GPIO_Init();  //hx711初始化
	Get_Tare();
	/*显示静态字符串*/
	OLED_ShowString(1, 1, "weight:");
	OLED_ShowString(1, 12, "g");
	OLED_ShowString(2, 1, "Angle:");
	while (1)
	{

		my_servo();
		Get_Weight();
		Lost_weight = 1000 - weight;
		OLED_ShowNum(1,8,weight,4);
		send_time++;
		if(send_time > 50)
		{
			esp8266_printf("#%d,%d#",weight,Lost_weight);
			Delay_ms(200);
      esp8266_printf("#%d,%d#",weight,Lost_weight);		
			send_time = 0;
		}
		Delay_ms(200);	
	}
}
